<?php
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------

namespace app\api\logic;

use \tpfcore\Core;
use think\Validate;
use think\Request;

class Cart extends ApiBase
{
    public function getCart($data=[]){
        $userInfo = $this->getBaseUserInfo();
        $result = Core::loadModel($this->name)->getList([
            "where"=>["user_id"=>$userInfo['id']],
            "order"=>"id desc"
        ]);

        //返回购物车数量
        if(!empty($data['type']) && $data['type']=="totalCartNumber"){
            return [0,"success",["number"=>count($result)]];
        }

        // 计算物品实际的价格
        foreach ($result as $key => $value) {
            $spec_key = $value['spec_key'];
            if($spec_key){
                $price = $value['goods_price'];
                $arr = explode(",", $spec_key);
                $listGoodsAttr = Core::loadModel("GoodsAttr")->getList([
                    "where"=>["id"=>["in",$arr]]
                ]);
                foreach ($listGoodsAttr as $k => $v) {
                    $price+=$v['attr_price'];
                }
                $result[$key]['price']=$price;
            }
        }

        // 商家，目前功能没有，后续完善，默认只有一个商家
        $seller=[
            0=>[
                "id"=>1,
                "name"=>"Tcms官方商城",
                "thumb"=>"https://gtms04.alicdn.com/tps/i4/TB1x4ExHVXXXXalXFXXSutbFXXX.jpg",
                "goods"=>$result
            ]
        ];

        return [0,"success",$seller];
    }
    public function opSelect($data=[]){
        $userInfo = $this->getBaseUserInfo();
        if(!empty($data['ids'])){
            self::updateObject(["user_id"=>$userInfo['id']],['is_select'=>0]);      // 全部取消选择
            self::updateObject(["user_id"=>$userInfo['id'],"id"=>["in",$data['ids']]],['is_select'=>1]);      // 全部取消选择
            return [0,"success"];
        }
        return [40046,"fail"];
    }
    public function opCart($data=[]){
        $type=empty($data['type'])?"":$data['type'];
        $userInfo = $this->getBaseUserInfo();
        switch ($type) {
            // 结算里库存操作
            case 'settlement_stock': 
                $listGoods = Core::loadAddonModel("Goods","mall","controller")->getList([
                    "field"=>"stock",
                    "where"=>["id"=>$data['goods_id']]
                ]);
                if($data['quantity']>$listGoods[0]['stock']){
                    return [40049,"库存不足"];
                }
                if(!empty($data['settlement_way']) && $data['settlement_way']=="cart"){
                    $result = self::updateObject(["id"=>$data['id'],"goods_id"=>$data['goods_id'],"user_id"=>$userInfo['id']],["number"=>$data['quantity']]);
                     if($result){
                        return [0,"success"];
                    }
                    return [40045,"操作失败"];
                }
                break;
            case 'delete':
                $result = self::deleteObject(["id"=>$data['id'],"user_id"=>$userInfo['id']],true);
                if($result){
                    return [0,"success"];
                }
                return [40045,"操作失败2222"];
            break;
            default:
                return [40049,"无效操作"];
                break;
        }
        return [0,"success"];
    }
    public function settlement($data=[]){
        $userInfo = $this->getBaseUserInfo();

        $type = empty($data['type'])?"cart":$data['type'];

        //购物车的方式结算
        if($type=="cart"){
            $result = Core::loadModel($this->name)->getList([
                "where"=>["user_id"=>$userInfo['id'],"is_select"=>1],
                "order"=>"id desc"
            ]);
            // 计算物品实际的价格
            foreach ($result as $key => $value) {
                $spec_key = $value['spec_key'];
                if($spec_key){
                    $price = $value['goods_price'];
                    $arr = explode(",", $spec_key);
                    $listGoodsAttr = Core::loadModel("GoodsAttr")->getList([
                        "where"=>["id"=>["in",$arr]]
                    ]);
                    foreach ($listGoodsAttr as $k => $v) {
                        $price+=$v['attr_price'];
                    }
                    $result[$key]['price']=$price;
                }
            }
        }
        else{  //直接购买的方式
            $result = Core::loadAddonModel("Goods","mall","controller")->getGoods([
                "field"=>"id,goods_sn,goods_name,goods_thumb,price,market_price",
                "where"=>["id"=>$data['id']]
            ]);
            if(empty($result)){
                return [40049,"没有要购买的产品"];
            }
            // 计算物品实际的价格
            foreach ($result as $key => $value) {
                $spec_key = $data['spec_key'];
                $result[$key]['goods_id']=$value['id'];
                if($spec_key){
                    $price = $value['price'];
                    $arr = explode(",", $spec_key);
                    $listGoodsAttr = Core::loadModel("GoodsAttr")->getList([
                        "where"=>["id"=>["in",$arr]]
                    ]);
                    foreach ($listGoodsAttr as $k => $v) {
                        $price+=$v['attr_price'];
                    }
                    $result[$key]['price']=$price;
                }
            }
        }
        
        // 查询地址
        if(empty($data['address_id'])){   //如果没有地址，取默认地址，没有默认地址，取第一条
            $listAddress = Core::loadModel("UserAddress")->getList([
                "where"=>["user_id"=>$userInfo['id']],
                "order"=>"is_default desc",
                "limit"=>1
            ]);
        }else{
            $listAddress = Core::loadModel("UserAddress")->getList([
                "where"=>["user_id"=>$userInfo['id'],"id"=>$data['address_id']]
            ]);
        }
        // 商家，目前功能没有，后续完善，默认只有一个商家
        $seller=[
            0=>[
                "id"=>1,
                "name"=>"Tcms官方商城",
                "thumb"=>"https://gtms04.alicdn.com/tps/i4/TB1x4ExHVXXXXalXFXXSutbFXXX.jpg",
                "goods"=>$result,
                "address"=>$listAddress
            ]
        ];
        return [0,"success",$seller];
    }
}